package com.example.administrator.myfragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

    private  MessageFragment messageFragment;
    private  ContactsFragment contactsFragment;
    private  NewsFragment newsFragment;
    private  SettingsFragment settingsFragment;
    private  View messageLayout;
    private  View contactsLayout;
    private View newsLayout;
    private View settingsLayout;
    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       initViews();
        fragmentManager=getFragmentManager();
        setTabSelection(0);
    }

    private  void initViews()
    {

        messageLayout=findViewById(R.id.message_layout);
        contactsLayout=findViewById(R.id.contacts_layout);
        newsLayout=findViewById(R.id.news_layout);
        settingsLayout=findViewById(R.id.setting_layout);
        messageLayout.setOnClickListener((View.OnClickListener) this);
        contactsLayout.setOnClickListener((View.OnClickListener) this);
        newsLayout.setOnClickListener((View.OnClickListener) this);
        settingsLayout.setOnClickListener((View.OnClickListener) this);

    }

/*
* 每个tab页对应的下标。0表示消息，1表示联系人，2表示动态，3表示设置。
* */
    private void setTabSelection(int index) {


     //每次选中之前先清除掉上次选中的状态
        clearSelection();
        //开启一个Fragment事务
        FragmentTransaction transaction =fragmentManager.beginTransaction();
        //先隐藏掉所有的Fragment 以防止多个fragment 显示在界面
        hideFragments(transaction);
        switch (index)
        {
            case 0:
                messageLayout.setBackgroundColor(0xff0000ff);
                if(messageFragment==null)
                {
                    //如果为空 则创建一个并添加到界面上
                    messageFragment=new MessageFragment();
                    transaction.add(R.id.content,messageFragment);
                }
                else {
                    transaction.show(messageFragment);
                }
                break;
            case 1:
                // 当点击了联系人tab时，改变控件的图片和文字颜色
                contactsLayout.setBackgroundColor(0xff0000ff);
                if (contactsFragment == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    contactsFragment = new ContactsFragment();
                    transaction.add(R.id.content, contactsFragment);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(contactsFragment);
                }
                break;
            case 2:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                newsLayout.setBackgroundColor(0xff0000ff);
                if (newsFragment == null) {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    newsFragment = new NewsFragment();
                    transaction.add(R.id.content, newsFragment);
                } else {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(newsFragment);
                }
                break;
            case 3:
            default:
                // 当点击了设置tab时，改变控件的图片和文字颜色
                settingsLayout.setBackgroundColor(0xff0000ff);
                if (settingsFragment == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    settingsFragment = new SettingsFragment();
                    transaction.add(R.id.content, settingsFragment);
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(settingsFragment);
                }
                break;
        }
        transaction.commit();

        }

//将所有fragment 都 设置为隐藏状态
    private void hideFragments(FragmentTransaction transaction) {
        if(messageFragment!=null)
        {
            transaction.hide(messageFragment);
        }
        if(contactsFragment!=null)
        {
            transaction.hide(contactsFragment);
        }
        if(newsFragment!=null)
        {
            transaction.hide(newsFragment);
        }
        if(settingsFragment!=null)
        {
            transaction.hide(settingsFragment);
        }

    }

    //清除掉所有状态
    private void clearSelection() {
        messageLayout.setBackgroundColor(0xffffffff);
        contactsLayout.setBackgroundColor(0xffffffff);
        newsLayout.setBackgroundColor(0xffffffff);
        settingsLayout.setBackgroundColor(0xffffffff);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public  void  onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.message_layout:
                setTabSelection(0);
                break;
            case R.id.contacts_layout:
                setTabSelection(1);
                break;
            case R.id.news_layout:
                setTabSelection(2);
                break;
            case R.id.setting_layout:
                setTabSelection(3);
                break;
        }
    }
}
